/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package genemodelsvg;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author nadaalhirabi
 */
public class GeneModelSVG {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
 
        
        // reading the file names from the user
        Scanner User_input=new Scanner(System.in);
        String file;
  
        System.out.print("Enter the extracted GFF file:");
        file=User_input.next();
       
     String token = "";    
     List<String> temps;
     int domlength=0;
        try (Scanner inFile = new Scanner(new File(file)).useDelimiter("\n")) {

            temps = new LinkedList<>();
            while (inFile.hasNext()) {
                // find next line
                token = inFile.next();            
                temps.add(token);
                domlength++;
            }   }

    String[] tempsDomArray = temps.toArray(new String[0]); 
    String[] filearray = new String[domlength];

     int b=0;
        for (String st : tempsDomArray) {//llop for all the file// copy to array
        filearray[b++]=st;
        }
    ////------------////     
            
    String[] GeneMsvgfile = new String[1];
    File dir = new File("GeneModelNormalizedSVG");
    dir.mkdir();

        for(int i=0; i<filearray.length;++i ){
                  //  for(int i=0; i<10;++i ){
            
            String[] parts=filearray[i].split("\t");
            String type=parts[0];//try to find gene type only
            // System.out.println(type);
            
            if(type.equals("gene")){//if it is gene we do loop to see the exons and draw them

                int normalizedGeneLength=300;
                int start = Integer.parseInt(parts[1]);
		int end = Integer.parseInt(parts[2]);
                int genelength= end-start;
                double Ysvg=20;
		String geneID= parts[3];
                
                
                //--------- write to svg file  : for each gene we fill the array 
                //then we send it to writefile : the name of the file is the geneID
                // <svg> and <line> are fixed for all genes
                //<rect> <line> for intron/exon  are changable
                GeneMsvgfile[0]="<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\" height=\"30\" width=\"301\">";
               // GeneMsvgfile[0]+="<line x1=\"0\" y1=\"25\" x2=\""+normalizedGeneLength+"\" y2=\"25\" style=\"stroke:green;stroke-width:2\" />";//creat the base line for the gene
                
             
                
                
                //// After ending the gene we chick the exon or domains that belong to it and it comes after it in the file-----------
                
                //Dom chicking -----------------------------------------------
                i++;
                String[]  parts2=filearray[i].split("\t");
                String type2=parts2[0];
                float normalizedLength=300;


                //mRNA chicking -----------------------------------------------
//                i++;
//                parts2=filearray[i].split("\t");
//                type2=parts2[0];
                 while(type2.equals("mRNA")){
                    ++i;
                    parts2=filearray[i].split("\t");
                    type2=parts2[0];  
                 }
                //exon chicking -----------------------------------------------         
                System.out.println(type2);
                parts2=filearray[i].split("\t");
                type2=parts2[0];
                
                if(type2.equals("exon")){  
                int Realstart =Integer.parseInt(parts2[1]);
                    while(type2.equals("exon")){  
                            double exstart =((double)(Integer.parseInt(parts2[1])- Realstart)/genelength)*normalizedLength;  // /5 because the gene line devided by 10
                            double exend = ((double)(Integer.parseInt(parts2[2])- Realstart)/genelength)*normalizedLength;
                            String parentGeneID= parts2[3];
                            double ExonLength= exend - exstart;
                            
                           //System.out.println(exstart+ "   "+ exend + "   "+ExonLength);
                            //draw exon
                            //int exonColor = document.getElementById("exonFilling").value;
                            //int exon1=svg.rect(exstart,Ysvg,ExonLength,10);
                            //exon1.attr({ fill: exonColor});
                            //GeneMsvgfile[0]+="  <rect x=\""+ exstart+ "\" y=\"20\" width=\""+   ExonLength +   "\" height=\"10\" style=\"fill:black ;stroke-width:;stroke:rgb(0,0,0)\" />" ;
                            GeneMsvgfile[0]+="  <rect x=\""+ exstart+ "\" y=\"20\" width=\""+   ExonLength +   "\" height=\"10\" />" ;

                            //read the next line to see if it is exon or not to draw the introns
                            ++i;
                            parts2=filearray[i].split("\t");
                            type2=parts2[0];
                            //draw intron : the link betweemn 2 exons
                            if(type2.equals("exon")){ 
                                double ex2_start =((double)Integer.parseInt(parts2[1])-Realstart)/genelength*300;
                                GeneMsvgfile[0]+="<line x1=\""+ exend+ "\" y1=\"20\" x2=\""+(double)(exend+ ((ex2_start-exend)/2))+"\" y2=\""+   (double) Ysvg/1.8 +   "\" style=\"stroke:#5e5a5d;stroke-width:2; stroke-dasharray:5,2 \" />";//creat intron
                                GeneMsvgfile[0]+="<line x1=\""+ (double)(exend+ ((ex2_start-exend)/2)) + "\" y1=\""+(double) Ysvg/1.8+"\" x2=\""+ex2_start+"\" y2=\""+   (double) Ysvg +   "\" style=\"stroke:#5e5a5d;stroke-width:2; stroke-dasharray:5,2 \" />";//creat intron
                            }
                            
                  }//end while(type.equals("exon"))
                GeneMsvgfile[0]+="</svg>";
                writeTofile(GeneMsvgfile, "./GeneModelNormalizedSVG/"+geneID+"_SVG");

                    }//end if exon
                
                //CDS chicking -----------------------------------------------
                if(type2.equals("CDS")){ continue; }//at the end it will exit Cuz the gen is done
  
//                GeneMsvgfile[0]+="</svg>";
//                writeTofile(GeneMsvgfile, "/Users/nadaalhirabi/Desktop/Cgene_Cgenome_V2/CGenesfile/A_acidus_CBS_106_47/GeneModelSVG/A_acidus_CBS_106_47_"+geneID);
                
            }//end if gene
            
    }//end loop on the array
        
        
  //System.out.println(GeneMsvgfile[0]);
         
    }//end main
    
    
public static void writeTofile(String[] digestedfile,  String filename) throws FileNotFoundException, IOException{
    
//System.out.println(digestedfile[0]);  

    BufferedWriter writer = null;
    try {
        File file = new File(filename+".svg");  
        PrintWriter out = new PrintWriter(new FileWriter(file));  

        // Write each string in the array on a separate line  
        for ( int i=0; i<digestedfile.length ;++i) {  
            String s =digestedfile[i];
            out.println(s);  
           //if(s.equals("##END"))break;
        }  
  
        out.close();  
    } catch(IOException ex) {
        ex.printStackTrace();
    }  
   
}//end write to file
}
